package com.asia.algorithmcode.str;



import java.util.Arrays;

/**
 * @DESCRIPTION: 87 ，扰乱字符串
 * @USER: wanfu
 * @DATE: 2025/4/15 星期二 16:59
 */
public class IsScramble {


    public static void main(String[] args) {
        String s1 = "eebaacbcbcadaaedceaaacadccd";
        String s2 = "eadcaacabaddaceacbceaabeccd";
        System.out.println(new IsScramble().isScramble(s1, s2));
    }

    public boolean isScramble(String s1, String s2) {
        if (s1.equals(s2)) {
            return true;
        }
        if (s1.length() != s2.length()) {
            return false;
        }

        byte[] b1 = s1.getBytes();
        byte[] b2 = s2.getBytes();
        Arrays.sort(b1);
        Arrays.sort(b2);

        if (!new String(b1).equals(new String(b2))) {
            return false;
        }

        int n = s1.length();
        for (int i = 1; i <= n - 1; i++) {
            // 不翻转的情况
            if (isScramble(s1.substring(0, i), s2.substring(0, i))
                    && isScramble(s1.substring(i), s2.substring(i))) {
                return true;
            }
            // 翻转的情况
            if (isScramble(s1.substring(0, i), s2.substring(n - i))
                    && isScramble(s1.substring(i), s2.substring(0, n - i))) {
                return true;
            }
        }
        return false;
    }

}
